import os
import subprocess

__author__ = 'Administrator'


def walklevel(some_dir, level=1):
    some_dir = some_dir.rstrip(os.path.sep)
    assert os.path.isdir(some_dir)
    num_sep = some_dir.count(os.path.sep)
    for root, dirs, files in os.walk(some_dir):
        yield root, dirs, files
        num_sep_this = root.count(os.path.sep)
        if num_sep + level <= num_sep_this:
            del dirs[:]


def do_start(paths):
    for root, dirs, files in paths:
        for project in dirs:
            project_dir = os.path.join(dev_path, project)
            project_sub_dirs = os.listdir(os.path.join(dev_path, project))
            for project_sub_dir in project_sub_dirs:
                if "tomcat" in project_sub_dir:
                    os.chdir(os.path.join(project_dir, project_sub_dir, "bin"))
                    os.system("startup.bat")
                elif "file" in project_sub_dir:
                    print(os.path.join(project_dir, project_sub_dir))
                    os.chdir(os.path.join(project_dir, project_sub_dir))
                    subprocess.call([os.path.join(project_dir, project_sub_dir, "start.bat")])


def raw_start(p_path):
    for first in os.listdir(p_path):
        project_path = os.path.join(p_path, first)
        for second in os.listdir(project_path):
            if "tomcat" in second:
                os.chdir(os.path.join(project_path, second, "bin"))
                subprocess.call([os.path.join(project_path, second, "bin", "startup.bat")])
            elif "file" in second:
                os.chdir(os.path.join(project_path, second))
                subprocess.Popen('start.bat', creationflags=subprocess.CREATE_NEW_CONSOLE)
                # os.system("start.bat")


dev_path = os.path.join("e:", os.path.sep, "dev", "wmb")
# gen = walklevel(dev_path, 0)

# do_start(gen)


raw_start(dev_path)
